package com.wang.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.common.utils.PageUtils;
import com.wang.common.utils.Query;
import com.wang.gulimall.product.dao.CategoryBrandRelationDao;
import com.wang.gulimall.product.entity.BrandEntity;
import com.wang.gulimall.product.entity.CategoryBrandRelationEntity;
import com.wang.gulimall.product.service.BrandService;
import com.wang.gulimall.product.service.CategoryBrandRelationService;
import com.wang.gulimall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private CategoryBrandRelationDao categoryBrandRelationDao;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 根据BrandId查询列表
     * @return
     */
    @Override
    public List<CategoryBrandRelationEntity> getListByBrandId(Long brandId) {
        QueryWrapper<CategoryBrandRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("brand_id",brandId);
        return this.list(queryWrapper);
    }

    /**
     * 新增品牌信息与分类信息关联
     * @param categoryBrandRelation
     */
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        categoryBrandRelation.setBrandName(brandService.getById(brandId).getName());
        Long catelogId = categoryBrandRelation.getCatelogId();
        categoryBrandRelation.setCatelogName(categoryService.getById(catelogId).getName());
        this.save(categoryBrandRelation);
    }

    /**
     * 根据brandId修改brandname
     * @param brandId
     * @param name
     */
    @Override
    public void updateBrandName(Long brandId, String name) {
        CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
        entity.setBrandId(brandId);
        entity.setBrandName(name);
        QueryWrapper<CategoryBrandRelationEntity> updateWrapper = new QueryWrapper<>();
        updateWrapper.eq("brand_id",brandId);
        this.update(entity, updateWrapper);
    }

    /**
     * 根据CategoryId修改CategoryName
     * @param catId
     * @param categoryName
     */
    @Override
    public void updateCategoryNameByCategoryId(Long catId, String categoryName) {
        categoryBrandRelationDao.updateCategoryNameByCategoryId(catId,categoryName);
    }

    /**
     * 获取分类关联的品牌
     * @param catId
     * @return
     */
    @Override
    public List<BrandEntity> getBrandsBycategoryId(Long catId) {
        QueryWrapper<CategoryBrandRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("catelog_id",catId);
        List<CategoryBrandRelationEntity> brandRelationEntities = baseMapper.selectList(queryWrapper);
        List<BrandEntity> collect = brandRelationEntities.stream().map(item -> {
            Long brandId = item.getBrandId();
            BrandEntity brandEntity = brandService.getById(brandId);
            return brandEntity;
        }).collect(Collectors.toList());
        return collect;
    }

}